package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.request.comment.ReplyUserCommentRequestBean;
import com.homedo.odin.solution.contract.mng.request.comment.UserCommentListRequestBean;
import com.homedo.odin.solution.contract.mng.response.comment.UserCommentDetailResponseBean;
import com.homedo.odin.solution.contract.pc.UserCommentListResponseBean;
import com.homedo.odin.solution.service.bizservice.mng.MngUserCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "评论接口", tags = {"Comment"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/comments")
public class MngUserCommentController extends BaseController<MngUserCommentService> {

    @GetMapping("/{commentId}")
    @ApiOperation(value = "OMS_查看评论详情", response = UserCommentDetailResponseBean.class, httpMethod = "GET")
    public ResultData queryUserCommentDetail(
            @ApiParam(value = "评论编号", required = true) @PathVariable(value = "commentId") Long commentId) {
        if (!NumberHelper.validate(commentId)) {
            throw new BizException(Meta.CODE_451.getCode(), "commentId非法");
        }

        UserCommentDetailResponseBean responseBean = baseService.queryUserCommentDetail(commentId);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @GetMapping
    @ApiOperation(value = "OMS_获取分页条件查询评论列表", notes = "OMS_获取分页条件查询评论列表", response = UserCommentListResponseBean.class, httpMethod = "GET")
    public ResultData queryUserCommentList(@Valid @ModelAttribute UserCommentListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }

        UserCommentListResponseBean responseBean = baseService.queryUserCommentList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @PutMapping("/{commentId}")
    @ApiOperation(value = "OMS_审核用户评论通过", httpMethod = "PUT")
    public ResultData auditUserCommentSuccess(
            @ApiParam(value = "评论编号", required = true) @PathVariable(value = "commentId") Long commentId) {
        if (!NumberHelper.validate(commentId)) {
            throw new BizException(Meta.CODE_451.getCode(), "评论ID非法");
        }
        baseService.auditUserCommentSuccess(commentId);
        return ResultData.newInstance();
    }

    @DeleteMapping("/{commentId}")
    @ApiOperation(value = "OMS_删除评论", httpMethod = "DELETE")
    public ResultData deleteUserComment(
            @ApiParam(value = "评论编号", required = true) @PathVariable(value = "commentId") Long commentId) {
        if (!NumberHelper.validate(commentId)) {
            throw new BizException(Meta.CODE_451.getCode(), "commentId非法");
        }

        baseService.updateUserComment(commentId);
        return ResultData.newInstance();
    }

    @PostMapping("/{commentId}")
    @ApiOperation(value = "OMS_回复评论", httpMethod = "POST")
    public ResultData replyUserComment(@ApiParam(value = "评论id", required = true)
    @PathVariable(value = "commentId") Long commentId,
            @Valid @RequestBody ReplyUserCommentRequestBean requestBean) {
        if (!NumberHelper.validate(commentId)) {
            throw new BizException(Meta.CODE_451.getCode(), "commentId非法");
        }
        baseService.replyUserComment(commentId, requestBean);
        return ResultData.newInstance();
    }


}
